package com.atguigu.yygh.hosp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 * @author 孔德成
 * @since 2021-08-27
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Override
    public Page<HospitalSet> pageQueryList(Long current, Long size, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> hospitalSetPage = new Page<>(current, size);
        // 条件查询的范型要填查询的结果集的类型，比如我要查询医院设置表 ，就填 HospitalSet
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        // AND (hosname LIKE ? AND hoscode = ?)
        // hosname=null Parameters: %null%(String), 10000(String)
        // hoscode=null Parameters: %协和%(String), null

        /*这里需要注意 hospitalSetQueryVo 空指针异常
         * 1. get 没有请求体，所以 @RequestBody 拿不到json数据，此时不会创建 HospitalSetQueryVo 实例，会出现空指针异常
         * 2. post 有请求体，即使前台没有传递json数据（空），也会创建 HospitalSetQueryVo 实例，此时，不会出现空指针异常
         * 3. 总结，只要有请求体，@RequestBody 就会生效，不会出现空指针异常
         * */
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        // 对参数进行非空检查，动态拼接非空条件
        // hosname 和 hoscode 全为空，则对所有数据进行分页返回
        /*org.springframework.util.StringUtils.isEmpty(Object str) {
            return str == null || "".equals(str)
        }
        */
        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like("hosname", hosname);
        }

        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hoscode);
        }

        return super.page(hospitalSetPage, queryWrapper);
    }

    @Override
    public boolean saveHospSet(HospitalSet hospitalSet) {
        /*
        1. id 由 mybatis plus 自动生成
        2. status(0),create_time,update_time,is_deleted(0) 有默认值，由 mysql 初始化
        3. sign_key 由后台生成
        4. hospitalSet 中包含：hosname, hoscode, api_url, contacts_name, contacts_phone, 数据库约束为 NULL
         */
        // 生成 sign_key 签名密钥
        String signKey = SecureUtil.md5("" + System.currentTimeMillis() + new Random().nextInt(1000));
        hospitalSet.setSignKey(signKey);
        return super.save(hospitalSet);
    }

    @Override
    public String getSignKey(String hoscode) {
        HospitalSet hospitalSet = baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        if (ObjectUtil.isNull(hospitalSet)) {
            // 如果没有查到医院信息，说明医院编号错误，该医院并没有注册，返回失败
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN.getCode(), ResultCodeEnum.HOSPITAL_OPEN.getMessage());
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public String getApiUrl(String hoscode) {
        return baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode)).getApiUrl();
    }
}
